/* eslint-disable */
import { FuncMap } from './FuncMap';
export class TplHoverProvider {
  // todo 这里是要提示k8s资源的，暂时不做
  // private resmap: CompletionMeta[];

  constructor() {
    const fm = new FuncMap();
    this.funcmap = fm.all();
    // this.valmap = fm.helmVals();
  }

  /**
   * hover展示效果。
   * 指定了位置和document，如果展示多个，会被merge
   */
  provideHover(model, position, token) {
    const iWord = model.getWordAtPosition(position);
    const word = iWord?.word ? iWord?.word : '';
    if (iWord == null) {
      return null;
    }
    if (iWord.word === '') {
      return null;
    }
    if (this.inActionVal(model, position, word)) {
      const found = this.findVal(iWord, position);
      if (found) {
        return {
          contents: found.contents,
          range: found.range,
        };
      }
    }
    if (this.inAction(model, position, word)) {
      const found = this.findFunc(iWord, position);
      if (found) {
        return {
          contents: found.contents,
          range: found.range,
        };
      }
    }
    // 这里是对yaml的进行提示
    if (this.notInAction(model, position, word)) {
      // 这里只提示 key，不提示v
      // 比如 replica: 1 ,悬浮在replica上有提示，在1上面就没有
      // todo 这里依赖比较重的yaml解析，先放一放不做
    }
    return null;
  }

  /**
   * 这里是判断是否在{{}}之内
   * @param model
   * @param position
   * @param word
   * @private
   */
  inActionVal(model, position, word) {
    const lineText = model.getLineContent(position.lineNumber);
    const r = RegExp(`{{[^}]*\\.(${word})[\\.\\s]?[^{]*}}`);
    return r.test(lineText);
  }

  inAction(model, position, word) {
    const lineText = model.getLineContent(position.lineNumber);
    const r = RegExp(`{{[^}]*[\\s\\(|]?(${word})\\s[^{]*}}`);
    return r.test(lineText);
  }

  notInAction(model, position, word) {
    const lineText = model.getLineContent(position.lineNumber);
    const r = new RegExp(`(^|})[^{]*(${word})`);
    return r.test(lineText);
  }

  // 判断是不是key
  isPositionInKey(model, position) {
    if (!model || !position) {
      return false;
    }
    return false;
  }

  findFunc(iWord, position) {
    for (const item of this.funcmap) {
      if (item.getLabel() === iWord.word) {
        return new TextHover({
          text: `${item.getLabel()}  \n ${item.getDetail()}`,
          start: iWord.startColumn,
          end: iWord.endColumn,
          line: position.lineNumber,
        });
      }
    }
    return undefined;
  }

  findVal(iWord, position) {
    for (const item of this.valmap) {
      if (item.getLabel() === iWord.word) {
        return new TextHover({
          text: `**${item.getLabel()}**  \n ${item.getDetail()}`,
          start: iWord.startColumn,
          end: iWord.endColumn,
          line: position.lineNumber,
        });
      }
    }
    return undefined;
  }
}

class TextHover {
  constructor({ text, start, end, line }) {
    this.contents = [{ value: text }];
    this.range = { startLineNumber: line, startColumn: start, endColumn: end, endLineNumber: line };
  }
}
